var def_lat = 49.44;
var def_lng = 32.06;
var def_zoom = 13;
var panOffset = 100;

var def_w = '500px';
var def_h = '300px';

var map;
var marker;

function getParameter(name)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return null;
  else
    return results[1];
}

function fillUrlsCommon(maplat, maplng, mapzoom, maptype, markerlat, markerlng) {
   var params = 'lat=' + maplat 
               + '&lng=' + maplng 
               + '&z=' + mapzoom
               + '&t=' + maptype;
   if (isMarkerVisible()) {
      params += '&mlat=' + markerlat 
              + '&mlng=' + markerlng;
   }
   //if (document.getElementById("map_canvas").style.width != def_w) {
      params += '&w=' + document.getElementById("map_canvas").style.width;
      params += '&h=' + document.getElementById("map_canvas").style.height;
   //}
   fillTheUrl(params, 'google-maps');
   fillTheUrl(params, 'google-maps-mobile');
   fillTheUrl(params, 'yandex-maps');
   fillTheUrl(params, 'visicom-maps');
   fillTextLinkUrl();
}

function fillTheUrl(params, urlname) {
   var a_elem = document.getElementById(urlname);
   if (a_elem.href.indexOf('?') > 0) {
      a_elem.href = a_elem.href.substring(0, a_elem.href.indexOf('?'))
   }
   a_elem.href = a_elem.href + '?' + params;
}

function fillTextLinkUrl() {
   var a_elem = location.href;
   var currentLink = null;
   if (a_elem.indexOf('google-maps-mobile') > 0) {
      currentLink = document.getElementById('google-maps-mobile').href;
   } else if (a_elem.indexOf('google-maps') > 0) {
      currentLink = document.getElementById('google-maps').href;
   } else if (a_elem.indexOf('yandex-maps') > 0) {
      currentLink = document.getElementById('yandex-maps').href;
   } else if (a_elem.indexOf('visicom-maps') > 0) {
      currentLink = document.getElementById('visicom-maps').href;
   } else {
      // default
      alert('There is no Map Service in the page location.');
      return;
   }
   document.getElementById('textlink').value = currentLink;
   document.getElementById('refresh').href = currentLink;
   
}

function markerButtons() {
   if (document.getElementById("setmarker").style.display == "none") {
      document.getElementById("setmarker").style.display = "block";
      document.getElementById("removemarker").style.display = "none";
   } else {
      document.getElementById("setmarker").style.display = "none";
      document.getElementById("removemarker").style.display = "block";
   }
}

function initStyleButtons() {
   styleButtons();
}

function styleButtons() {
   if (document.getElementById("newstyle").style.display != "none") {
      // get new style
      document.getElementById("newstyle").style.display = "none";
      document.getElementById("oldstyle").style.display = "block";
      document.getElementById("map_links").style.display = "block";
      document.getElementById("map_links_old").style.display = "none";
   } else {
      // get old style
      document.getElementById("newstyle").style.display = "block";
      document.getElementById("oldstyle").style.display = "none";
      document.getElementById("map_links").style.display = "none";
      document.getElementById("map_links_old").style.display = "block";
   }
}

function initMarkerButtons(visibleMarker) {
   if (visibleMarker) {
      document.getElementById("setmarker").style.display = "none";
      document.getElementById("removemarker").style.display = "block";
   } else {
      document.getElementById("setmarker").style.display = "block";
      document.getElementById("removemarker").style.display = "none";
   }
}

function resizeMapCanvas(w, h) {
   document.getElementById("map_canvas").style.width = w;
   document.getElementById("map_canvas").style.height = h;

   document.getElementById("div_url").style.width = w;
   document.getElementById("map_links").style.width = w;
}

function initMapCanvasSize() {
   var w = getParameter('w');
   var h = getParameter('h');
   if (w != null || h != null) {
      if (w == null)
         w = def_w;
      if (h == null)
         h = def_h;
      resizeMapCanvas(w, h);
   }
}


